package com.appoint.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.entity.ContentType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;

@Slf4j
public class CreateMultipartFile {

    public static MultipartFile createMfileByPath(String path) {
        MultipartFile mFile = null;
        try {
            File file = new File(path);
            if(file.exists()){
                FileInputStream fileInputStream = new FileInputStream(file);
                String fileName = file.getName();
                fileName = fileName.substring((fileName.lastIndexOf("/") + 1));
                mFile =  new MockMultipartFile(fileName, fileName, ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
            }else{
              return null;
            }
        } catch (Exception e) {
            log.error("封装文件出现错误：{}", e);
        }
        return mFile;
    }
}
